﻿$(document).ready(function () {
    fillComboPerfil() ;
    fillValuesByProfile(1);
    $("#operation_profile").change(function () {
        fillValuesByProfile($("#operation_profile").val());
    });
    $("#guardar").click(function () {
        saveMatrix();
    });
});

function fillComboPerfil() {
    $.ajax({
        type: "POST",
        //data: JSON.stringify(),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "getProfiles",
        success: function (data) {
            addProfiles(data);
        }
    });
};

function fillValuesByProfile(profile) {
    clearRules();
    var profileId = { profileId: profile };
    $.ajax({
        type: "POST",
        data: JSON.stringify(profileId),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "getRulesByProfile",
        success: function (data) {
            addProfileRule(data);
        }
    });
}

function saveMatrix() {
    var data = [];
    $("#rules_profile tr").each(function () {
        var values = $(this).children();
        var row = {
            profileId: $(values[5]).text(),
            sectorId: $(values[6]).text(),
            productId: $(values[7]).text(),
            maximalAmount: $(values[3]).find("input").val(),
            minimumRate: $(values[ 4 ]).find( "input" ).val()
        };
        data.push(row);
    });
    saveRulesInBD(data);
}

function addProfiles(data) {
    for (var i = 0 ; i < data.length ; i++)
        $("#operation_profile").append($("<option>").attr("value", data[i].idperfil + "").text(data[i].nombreperfil + ""));
}

function clearRules() {
    $("#rules_profile").empty();
}

function addProfileRule(data) {
    for( var i = 0 ; i < data.length ; i++){
        var rule = data[i];
        $("#rules_profile").append("<tr>" + 
            //"<td idperfil = " + rule.perfil.idperfil + ">" + rule.perfil.nombreperfil + "</td>" +
            //"<td idsector = " + rule.sector.idsector + ">" + rule.sector.nombre + "</td>" +
            //"<td idproducto = " + rule.producto.idproducto + ">" + rule.producto.nombre + "</td>" +
            "<td>" + rule.perfil.nombreperfil + "</td>" +
            "<td>" + rule.sector.nombre + "</td>" +
            "<td>" + rule.producto.nombre + "</td>" +
            // MONTO MAXIMO
            '<td class="center"><div class="controls"><div class="input-prepend"><span class="add-on"><</span>' +
            '<input style="height: 33px;" class="m-wrap " type="text" placeholder="monto" value=' + rule.montomaximo + '></div></div>' + "</td>" +
            // TASA MINIMA
            '<td class="center"><div class="controls"><div class="input-prepend"><span class="add-on">></span>' +
            '<input style="height: 33px;" class="m-wrap " type="text" placeholder="tasa" value=' + rule.tasaminima + "></div></div>" + "</td>" +
            // IDS
            "<td hidden>" + rule.perfil.idperfil + "</td>" +
            "<td hidden>" + rule.sector.idsector + "</td>" +
            "<td hidden>" + rule.producto.idproducto + "</td>" +
            "</tr>");
    }
}

function saveRulesInBD(rules) {
    $.ajax({
        type: "POST",
        data: JSON.stringify(rules),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "saveRulesByProfile",
        success: function (data) {
            alert( "ok" ) ;
        }
    });;
}